package mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBC_Druid {
    static DataSource ds = null;

    public static DataSource getDs() {
        return ds;
    }

    static{
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\Druid.properties"));

            ds = DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(ResultSet set, Connection con, Statement sta){
        try {
            if(set != null){
                set.close();
            }
            if(con != null){
                con.close();
            }
            if(sta != null){
                sta.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
